package deploy

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/deploy"
	deployReq "github.com/flipped-aurora/gin-vue-admin/server/model/deploy/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type DeployAuthorizedApi struct{}

// CreateDeployAuthorized 创建授权管理
// @Tags DeployAuthorized
// @Summary 创建授权管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body deploy.DeployAuthorized true "创建授权管理"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /deployAuthorized/createDeployAuthorized [post]
func (deployAuthorizedApi *DeployAuthorizedApi) CreateDeployAuthorized(c *gin.Context) {
	var deployAuthorized deploy.DeployAuthorized
	err := c.ShouldBindJSON(&deployAuthorized)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = deployAuthorizedService.CreateDeployAuthorized(&deployAuthorized)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteDeployAuthorized 删除授权管理
// @Tags DeployAuthorized
// @Summary 删除授权管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body deploy.DeployAuthorized true "删除授权管理"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /deployAuthorized/deleteDeployAuthorized [delete]
func (deployAuthorizedApi *DeployAuthorizedApi) DeleteDeployAuthorized(c *gin.Context) {
	id := c.Query("id")
	err := deployAuthorizedService.DeleteDeployAuthorized(id)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteDeployAuthorizedByIds 批量删除授权管理
// @Tags DeployAuthorized
// @Summary 批量删除授权管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /deployAuthorized/deleteDeployAuthorizedByIds [delete]
func (deployAuthorizedApi *DeployAuthorizedApi) DeleteDeployAuthorizedByIds(c *gin.Context) {
	ids := c.QueryArray("ids[]")
	err := deployAuthorizedService.DeleteDeployAuthorizedByIds(ids)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateDeployAuthorized 更新授权管理
// @Tags DeployAuthorized
// @Summary 更新授权管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body deploy.DeployAuthorized true "更新授权管理"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /deployAuthorized/updateDeployAuthorized [put]
func (deployAuthorizedApi *DeployAuthorizedApi) UpdateDeployAuthorized(c *gin.Context) {
	var deployAuthorized deploy.DeployAuthorized
	err := c.ShouldBindJSON(&deployAuthorized)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = deployAuthorizedService.UpdateDeployAuthorized(deployAuthorized)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindDeployAuthorized 用id查询授权管理
// @Tags DeployAuthorized
// @Summary 用id查询授权管理
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query deploy.DeployAuthorized true "用id查询授权管理"
// @Success 200 {object} response.Response{data=deploy.DeployAuthorized,msg=string} "查询成功"
// @Router /deployAuthorized/findDeployAuthorized [get]
func (deployAuthorizedApi *DeployAuthorizedApi) FindDeployAuthorized(c *gin.Context) {
	id := c.Query("id")
	redeployAuthorized, err := deployAuthorizedService.GetDeployAuthorized(id)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
		return
	}
	response.OkWithData(redeployAuthorized, c)
}

// GetDeployAuthorizedList 分页获取授权管理列表
// @Tags DeployAuthorized
// @Summary 分页获取授权管理列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query deployReq.DeployAuthorizedSearch true "分页获取授权管理列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /deployAuthorized/getDeployAuthorizedList [get]
func (deployAuthorizedApi *DeployAuthorizedApi) GetDeployAuthorizedList(c *gin.Context) {
	var pageInfo deployReq.DeployAuthorizedSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := deployAuthorizedService.GetDeployAuthorizedInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetDeployAuthorizedPublic 不需要鉴权的授权管理接口
// @Tags DeployAuthorized
// @Summary 不需要鉴权的授权管理接口
// @accept application/json
// @Produce application/json
// @Param data query deployReq.DeployAuthorizedSearch true "分页获取授权管理列表"
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /deployAuthorized/getDeployAuthorizedPublic [get]
func (deployAuthorizedApi *DeployAuthorizedApi) GetDeployAuthorizedPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的授权管理接口信息",
	}, "获取成功", c)
}
